package com.example.web.demos.controller;

import com.alibaba.fastjson.JSON;
import com.example.web.demos.dao.ConfigRepository;
import com.example.web.demos.domain.dto.CostModelConfig;
import com.example.web.demos.domain.dto.Employer;
import com.example.web.demos.feign.CDApiClient;
import com.example.web.demos.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: scout.du
 * @date: 2024/06/19 17:57
 **/
@RestController
public class HelloController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    CDApiClient cdApiClient;

    @Autowired
    CostModelConfig costModelConfig;

    @Autowired
    ConfigRepository configRepository;

//    @Autowired
//    @Qualifier("restTemplate1")
//    RestTemplate restTemplate1;

    @GetMapping("/hello")
    public ResponseEntity hello(){

        return new ResponseEntity<>("HELLO",HttpStatus.OK);
    }

    @GetMapping("/hello5")
    public ResponseEntity hello5(){

        List<String> prefixs = configRepository.getPrefixs();
        return new ResponseEntity<>(prefixs,HttpStatus.OK);
    }


    @GetMapping("/hello4")
    public ResponseEntity hello4(){

        System.out.println(costModelConfig);
        return new ResponseEntity<>("HELLO",HttpStatus.OK);
    }

    @PostMapping("/hello2")
    public Employer hello2(){

        Employer employer = new Employer("tom","aa",LocalDateTime.now());
        System.out.println(employer);
        System.out.println(JSON.toJSONString(employer));
        return employer;
    }

    @GetMapping("/user-apps/clearCache")
    public String hello1(){

        return "hello";
    }

    @GetMapping("/request1")
    public String request1(){

//        int timeout = 3000; // 超时时间，单位为毫秒
//
//        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
//        factory.setConnectTimeout(timeout);
//        factory.setReadTimeout(timeout);
//
//        restTemplate.setRequestFactory(factory);


        System.out.println(DateUtils.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss:SSS"));
        String response = restTemplate.getForObject("http://127.0.0.1:8080/health", String.class);
        System.out.println(response);
        System.out.println(DateUtils.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss:SSS"));
        return "hello";
    }


    @GetMapping("/request2")
    public String request2(){

        System.out.println(DateUtils.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss:SSS"));
        String response = restTemplate.getForObject("http://127.0.0.1:8080/health", String.class);
        System.out.println(response);
        System.out.println(DateUtils.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss:SSS"));
        return "hello";
    }

    @GetMapping("/request3")
    public String request3(){

        cdApiClient.test2("tom");
        return "hello";
    }

}
